<template>
  <AppNavBar> 个人信息 </AppNavBar>
  <div class="fill"></div>
  <div class="out" v-if="!$store.state.user.isLogin">
    <i class="iconfont icon-weidenglu"></i>
    <p>还未登录哦！快来抢占沙发吧！</p>
  </div>
  <div class="user" v-else-if="user">
    <div class="user-info">
      <Image
        class="user-avatar"
        round
        fit="cover"
        width="18vw"
        height="18vw"
        src="https://img.yzcdn.cn/vant/cat.jpeg"
      />
      <div class="user-detail">
        <div class="user-name">昵称：{{ user.name }}</div>
        <div class="user-email">邮箱：{{ user.email }}</div>
        <div class="user-signature">个性签名：永远相信美好的事情即将发生！</div>
      </div>
    </div>
    <div class="user-operations">
      <div class="user-operation" @click="$router.push('/collects')">
        <span>我的收藏</span>
        <i class="iconfont icon-you"></i>
      </div>
      <div class="user-operation" @click="$router.push('/orders')">
        <span>我的订单</span>
        <i class="iconfont icon-you"></i>
      </div>
      <div class="user-operation">
        <span>账号管理</span>
        <i class="iconfont icon-you"></i>
      </div>
      <div class="user-operation" @click="$router.push('/address')">
        <span>地址管理</span>
        <i class="iconfont icon-you"></i>
      </div>
      <div class="user-operation">
        <span>关于我们</span>
        <i class="iconfont icon-you"></i>
      </div>
    </div>
  </div>
  <div class="btn-c">
    <Button
      v-if="$store.state.user.isLogin"
      round
      color="#42b983"
      @click="logoutFn"
      size="normal"
    >
      退出登录
    </Button>
    <Button
      v-else
      round
      color="#42b983"
      @click="$router.push({ path: '/login', query: { from: '/profile' } })"
      size="normal"
    >
      登录
    </Button>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import AppNavBar from '@/components/library/AppNavBar.vue'
import { logout, getUserInfo } from '@/api/user'
import { Button, Toast, Image } from 'vant'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'Profile',
  components: { AppNavBar, Button, Image },
  setup () {
    const store = useStore()
    const route = useRoute()
    // 判断是否登录
    const user = ref(null)
    // 如果已经登陆则请求个人数据
    watch(() => route.path, (newVal) => {
      if (store.state.user.isLogin && route.path === '/profile') {
        getUserInfo().then(res => {
          user.value = res.data
        })
      }
    }, { immediate: true })
    const logoutFn = () => {
      logout().then(res => {
        if (res.status === 204) {
          // 退出时清除本地缓存
          Toast.success('退出成功！')
          window.localStorage.removeItem('token')
          store.commit('setUser', { isLogin: false })
        }
      })
    }
    return {
      logoutFn,
      user
    }
  }
}
</script>

<style scoped lang='less'>
.fill {
  height: 53px;
  width: 100%;
}
.out {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .icon-weidenglu {
    color: #eee;
    font-size: 200px;
  }
}
.btn-c {
  position: absolute;
  bottom: 70px;
  left: 0;

  Button {
    transform: translateX(25vw);
    width: 50vw;
  }
}
.user {
  .user-info {
    display: flex;
    padding: 20px;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #eee;
    margin-bottom: 30px;
    .user-avatar {
      display: block;
      margin-right: 10px;
    }
    .user-detail {
      flex: 1;
      .user-signature {
        margin-top: 5px;
        width: 70vw;
        white-space: nowrap;
        /*溢出部分文字隐藏*/
        overflow: hidden;
        /*溢出部分省略号处理*/
        text-overflow: ellipsis;
      }
    }
  }
  .user-operations {
    .user-operation {
      display: flex;
      justify-content: space-between;
      height: 46px;
      font-weight: 600;
      width: 95vw;
      margin: 5px 10px;
      padding: 0 5px;
      align-items: center;
      border-radius: 8px;
      background-color: rgb(246, 246, 246);
      &:hover {
        background-color: #eee;
      }
    }
  }
}
</style>
